package com.google.android.apps.wakemewhere;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;

public class Alarm {
	private long id = -1L;
	private boolean enabled = true;
	private boolean vibrate = true;
	private boolean exited = true;  
	private String label = "";
	private long latitudeE6 = 0;
	private long longitudeE6 = 0;
	
	public Alarm() {
	}

	public Alarm(long id, boolean enabled, boolean exited, boolean vibrate, String label, long latitudeE6,
			long longitudeE6) {
		this.id = id;
		this.enabled = enabled;
		this.label = label;
		this.latitudeE6 = latitudeE6;
		this.longitudeE6 = longitudeE6;
		this.exited = exited;
		this.vibrate = vibrate;
	}

	public Alarm(Cursor cursor) {
		this(cursor.getLong(cursor.getColumnIndexOrThrow("_id")), cursor
				.getInt(cursor.getColumnIndexOrThrow("enabled")) == 1, cursor
				.getInt(cursor.getColumnIndexOrThrow("exited")) == 1, cursor
				.getInt(cursor.getColumnIndexOrThrow("vibrate")) == 1, cursor
				.getString(cursor.getColumnIndexOrThrow("label")), cursor
				.getLong(cursor.getColumnIndexOrThrow("latitude_e6")), cursor
				.getLong(cursor.getColumnIndexOrThrow("longitude_e6")));
	}

	public void writeOnto(ContentValues map) {
		map.put("label", label);
		map.put("latitude_e6", latitudeE6);
		map.put("longitude_e6", longitudeE6);
		map.put("enabled", enabled ? 1 : 0);
		map.put("exited", exited ? 1 : 0);
		map.put("vibrate", vibrate ? 1 : 0);
	}

	public boolean isEnabled() {
		return enabled;
	}

	public String getLabel() {
		return label;
	}

	public long getId() {
		return id;
	}
	
	public Location getLocation() {
		Location location  = new Location(LocationManager.NETWORK_PROVIDER);
		location.setLatitude(getLatitude());
		location.setLongitude(getLongitude());
		return location;
	}

	public void setLocation(long latitudeE6, long longitudeE6) {
		this.latitudeE6 = latitudeE6;
		this.longitudeE6 = longitudeE6;
	}

	public void setLabel(String value) {
		label = value;
	}

	public void setEnabled(boolean value) {
		enabled = value;
	}
	
	public void setExited(boolean value) {
		exited = value;
	}
	
	public boolean hasExited() {
		return exited;
	}

	public boolean shouldVibrate() {
		return vibrate;
	}
	
	public void setVibrate(boolean shouldVibrate) {
		vibrate = shouldVibrate;
	}

	public long getLatitudeE6() {
		return latitudeE6;
	}

	public double getLatitude() {
		return latitudeE6 / 1000000.0;
	}

	public long getLongitudeE6() {
		return longitudeE6;
	}

	public double getLongitude() {
		return longitudeE6 / 1000000.0;
	}

	public boolean triggersOn(Location location) {
		float distanceTo = location.distanceTo(location);
		return distanceTo < 100.0f;
	}

	public Uri asUri() {
		return ContentUris.withAppendedId(Alarms.CONTENT_URI, id);
	}

	public void writeOnto(Intent intent) {
		intent.setData(asUri());
		intent.putExtra(IntentExtras.ALARM_ID, id);
	}

	public static String locationSummaryString(long latitudeE6, long longitudeE6) {
		return "(" + latitudeE6 / 1000000.0 + "," + longitudeE6 / 1000000.0 + ")";
	}

	public String getLocationSummary() {
		return locationSummaryString(latitudeE6, longitudeE6);
	}
}
